﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace FancyBattles.GameAudio
{
    /// <summary>
    /// Audio manager for playing music and sound effects.
    /// <example>AudioManager.[method]([audio filename])</example>    
    /// Every GameScreen or derived classes instance has a reference to the AudioManager.
    /// Multiple sound effects can be played, while only one song (music) can be played at each time;
    /// </summary>
    class AudioManager : GameComponent
    {       
        /// <summary>
        /// Empty constructor. 
        /// AudioManager instances are prepared by GameScreen instances.
        /// </summary>
        /// <param name="game">A reference to the game, useful for getting game content information.</param>
        public AudioManager(Game game) : base(game)
        {            
        }

        /// <summary>
        /// Play a sound effect once.
        /// </summary>
        /// <param name="name">Asset name which represents the sound file.</param>
        public void PlaySound(String name)
        {
            SoundEffect sound = this.Game.Content.Load<SoundEffect>(name);
            sound.Play();            
        }

        /// <summary>
        /// Private method for playing music. It does not specify whether it should loop or not.
        /// </summary>
        /// <param name="name">Asset name which represents the sound file.</param>
        private void PlayMusic(String name)
        {            
            StopMusic();
            Song song = this.Game.Content.Load<Song>(name);
            MediaPlayer.Play(song);            
        }

        /// <summary>
        /// Play a music once.
        /// </summary>
        /// <param name="name">Asset name which represents the sound file.</param>
        public void PlayMusicOnce(String name)
        {
            MediaPlayer.IsRepeating = false;
            PlayMusic(name);            
        }

        /// <summary>
        /// Play a music repeatedly, being necessary to manually stop it.
        /// </summary>
        /// <param name="name">Asset name which represents the sound file.</param>
        public void PlayMusicLoop(String name)
        {
            MediaPlayer.IsRepeating = true;         
            PlayMusic(name);            
        }

        /// <summary>
        /// Manually stop playing the current music.
        /// </summary>
        public void StopMusic()
        {
            MediaPlayer.Stop();
        }
    }
}
